<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
require('include.php');
use Admin\Controller\Tencentyun\Conf;
use Admin\Controller\Tencentyun\ImageV2;
use Admin\Controller\Tencentyun\Auth;
use Admin\Controller\Tencentyun\ImageProcess;
use Think\Model;

class LiveCompetitorController extends AdminbaseController {
	
	private $live_competitor;
	
	function _initialize() {
		parent::_initialize();
		$this->live_competitor = D("Common/LiveCompetitor");
	}
	
	function index(){
		$id = I("post.id", '');
		$sex = I("post.sex", -1);
		$name = I("post.name", "");
		$nickname = I("post.nickname", "");
		$count = $this->live_competitor->queryLiveCompetitorCount($id, $sex, $name, $nickname);
		$page = $this->page($count, 20);
		
		$result = $this->live_competitor->queryLiveCompetitorList($id, $sex, $name, $nickname, $page->firstRow , $page->listRows);
		if(!empty($result)){
			$user_prop = M("vrlive.live_user_prop",null);
			foreach ($result as $key=>$val){
				$prop = $user_prop->where(array('competitor_id'=>$val['competitor_id']))->count();
				$result[$key]['prop_count'] = $prop;
			}
		}
		$this->assign("data", $result);
		$this->assign("formParam", array('id'=>$id, 'sex'=>$sex,'name'=>$name,'nickname'=>$nickname));
		$this->assign("page", $page->show('Admin'));
		$this->display();
	}
	
	function add(){
		$this->display();
	}
	
	function add_post(){
		if(IS_POST){
			$_POST['creator_id'] = $_SESSION["ADMIN_ID"];
			if(isset($_POST['weight']) && (empty($_POST['weight']) || '' == $_POST['weight'])){
				$_POST['weight'] = null;
			}
			if(isset($_POST['popular_value']) && (empty($_POST['popular_value']) || '' == $_POST['popular_value']) ){
				$_POST['popular_value'] = null;
			}
			$this->upload_avatar();
			
			if ($this->live_competitor->create()) {
				$result = $this->live_competitor->add();
				if ($result!==false) {
					$this->success("添加成功！", U("Admin/LiveCompetitor/index"));
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->live_competitor->getError());
			}
			
		}
	}
	
	private function upload_avatar(){
		if(!empty($_FILES) || isset($_FILES['imageFile'])){
			$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize   =     59614161 ;// 设置附件上传大小
			$upload->exts      =     array('jpeg', 'jpg', 'bmp', 'png');// 设置附件上传类型
			$upload->rootPath  =     C('UPLOADPATH'); // 设置附件上传根目录
			$upload->savePath  =     ''; // 设置附件上传（子）目录
		
			// 上传文件
			$info = $upload->upload();
			if(!$info) {// 上传错误提示错误信息
				$this->error($upload->getError());
			}else{
				$cname = C('UPLOADPATH').$info['imageFile']['savepath'].$info['imageFile']['savename'];
				$fileName = 'lv'.time().$info['imageFile']['savename'];  // 自定义文件名
				$uploadRet = ImageV2::upload($cname, Conf::BUCKET, $fileName);
				\Think\Log::record('上传封面万象优图返回：'.json_encode($uploadRet), 'INFO');
				if($uploadRet['httpcode'] == 200){
					$imageUrl = $uploadRet['data']['downloadUrl'];
					$_POST['avatar'] = $imageUrl;
				}
				//删除本地文件
				unlink($cname);
			}
		}
		$_POST['imageFile'] = null;
	}
	
    function edit(){
    	$competitor_id = I("get.competitor_id", 0);
    	$data = $this->live_competitor->find($competitor_id);
    	$this->assign($data);
		$this->display();
	}
	
	function edit_post(){
		if(isset($_POST['weight']) && (empty($_POST['weight']) || '' == $_POST['weight'])){
			$_POST['weight'] = null;
		}
		if(isset($_POST['popular_value']) && (empty($_POST['popular_value']) || '' == $_POST['popular_value']) ){
			$_POST['popular_value'] = null;
		}
		$this->upload_avatar();
		if ($this->live_competitor->create()) {
			$result = $this->live_competitor->save();
			if ($result !== false) {
				$this->success("保存成功！", U("Admin/LiveCompetitor/index"));
			} else {
				$this->error("保存失败！");
			}
		} else {
			$this->error($this->live_competitor->getError());
		}
	}
	
	function delete(){
		$competitor_id = I("get.competitor_id", 0);
		$status = 0;
		$postData = array(
			'status'=>$status
		);
		$result = $this->live_competitor->where(array('competitor_id'=>$competitor_id))->save($postData);
		if($result){
			$this->success("删除成功！", U("Admin/LiveCompetitor/index"));
		}else{
			return $this->error("删除失败！");
		}
	}

	/**
	 * 送道具人数管理
	 */
	public function prop_info(){
		$competitor_id = I("competitor_id");
		$live_id       = I("live_id");
		$pk_id         = I("pk_id");
		$start_time    = I("post.start_time");
		$end_time      = I("post.end_time");
		$phone         = I("post.phone");
		$nickname      = I("post.nickname");
		$start_num     = I("post.start_num");
		$end_num       = I("post.end_num");
		$start_value   = I("post.start_value");
		$end_value     = I("post.end_value");
		$where = " 1=1 ";
		if(!empty($competitor_id)){
			$where .= " and live_user_prop.competitor_id = '".$competitor_id."'";
		}
		if(!empty($live_id)){
			$where .= " and live_user_prop.live_id = '".$live_id."'";
		}
		if(!empty($pk_id)){
			$where .= " and live_user_prop.pk_id = '".$pk_id."'";
		}
		if(!empty($start_time)){
			$where .= " and live_user_prop.create_time >= '".$start_time."'";
		}
		if(!empty($end_time)){
			$where .= " and live_user_prop.create_time < '".$end_time."'";
		}
		if(!empty($phone)){
			$where .= " and user.mobile_phone = '".$phone."'";
		}
		if(!empty($nickname)){
			$where .= " and user.nickname = '".$nickname."'";
		}
		if(!empty($start_num)){
			$where .= " and live_user_prop.prop_cnt >= '".$start_num."'";
		}
		if(!empty($end_num)){
			$where .= " and live_user_prop.prop_cnt < '".$end_num."'";
		}
		if(!empty($start_value)){
			$where .= " and live_user_prop.sentiment_value >= '".$start_value."'";
		}
		if(!empty($end_value)){
			$where .= " and live_user_prop.sentiment_value < '".$end_value."'";
		}
		if($competitor_id){
			$competitor_info = $this->live_competitor->where(array('competitor_id'=>$competitor_id))->find();
			if(!$competitor_info){
				return $this->error("选手不存在！");
			}
		}
		$user_prop = M("vrlive.live_user_prop",null);
		$prop_list = $user_prop->join('vruser1.user on user.user_id = live_user_prop.user_id','left')
					->join('vrlive.live_prop on live_prop.prop_id = live_user_prop.prop_id','left')
					->where($where)
					->field('live_user_prop.*, live_prop.name,user.nickname,user.mobile_phone')
					->select();
		$this->assign('list', $prop_list);
		$this->assign('competitor_id', $competitor_id);
		$this->display('prop_info');
	}
}